package com.vfa.fishing;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.IBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.animator.SlideMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;

import com.vfa.fishing.BaseMainMenu;
import com.vfa.fishing.common.AbstractObject;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.DisplayMetrics;
import android.view.KeyEvent;


public class MenuActivity extends BaseMainMenu implements IOnMenuItemClickListener
{
    // ===========================================================
    // Constants
    // ===========================================================
    public static final String PREFS_NAME = "MyPrefsFile";

    public static final int CAMERA_WIDTH = 800;
    public static final int CAMERA_HEIGHT = 480;

    protected static final int MENU_START = 0;
    protected static final int MENU_LOAD = MENU_START + 1;
    protected static final int MENU_HELP = MENU_LOAD + 1;
    protected static final int MENU_SETTING = MENU_HELP + 1;
    protected static final int MENU_SCORE = MENU_SETTING + 1;
    protected static final int MENU_QUIT = MENU_SCORE + 1;
    protected static final int MENU_QUIT_OK = MENU_QUIT + 1;
    protected static final int MENU_QUIT_BACK = MENU_QUIT_OK + 1;

    // ===========================================================
    // Fields
    // ===========================================================

    protected Camera mCamera;

    protected Scene mMainScene;

    private Texture mTexture;

    protected MenuScene mMenuScene;

    private Texture mMenuTexture;
    private Texture mBackgroundTexture;
    
    protected TextureRegion mBackgroundTextureRegion;
    protected TextureRegion mMenuStartTextureRegion;
    protected TextureRegion mMenuLoadTextureRegion;
    protected TextureRegion mMenuHelpTextureRegion;
    protected TextureRegion mMenuSettingTextureRegion;
    protected TextureRegion mMenuScoreTextureRegion;
    protected TextureRegion mMenuQuitTextureRegion;

    private MenuScene mSubMenuScene;

    private Texture mSubMenuTexture;
    private TextureRegion mMenuOkTextureRegion;
    private TextureRegion mMenuBackTextureRegion;

    public ArrayList<AbstractObject > arrAllObject ;

    @Override
    public Engine onLoadEngine() {
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }

    @Override
    public void onLoadResources() {
        this.mTexture = new Texture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mEngine.getTextureManager().loadTexture(this.mTexture);
        
        this.mBackgroundTexture = new Texture(1024, 1024, TextureOptions.DEFAULT);
        this.mBackgroundTextureRegion = TextureRegionFactory.createFromAsset(this.mBackgroundTexture, this, "gfx/background_menu.jpg", 0, 0);

        this.mMenuTexture = new Texture(256, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mMenuStartTextureRegion = TextureRegionFactory.createFromAsset(this.mMenuTexture, this, "gfx/menu_start.png", 0, 0);
        this.mMenuLoadTextureRegion = TextureRegionFactory.createFromAsset(this.mMenuTexture, this, "gfx/menu_load.png", 0, 50);
        this.mMenuHelpTextureRegion = TextureRegionFactory.createFromAsset(this.mMenuTexture, this, "gfx/menu_help.png", 0, 100);
        this.mMenuSettingTextureRegion = TextureRegionFactory.createFromAsset(this.mMenuTexture, this, "gfx/menu_setting.png", 0, 150);
        this.mMenuScoreTextureRegion = TextureRegionFactory.createFromAsset(this.mMenuTexture, this, "gfx/menu_score.png", 0, 200);
        this.mMenuQuitTextureRegion = TextureRegionFactory.createFromAsset(this.mMenuTexture, this, "gfx/menu_quit.png", 0, 250);
        this.mEngine.getTextureManager().loadTexture(this.mMenuTexture);

        this.mSubMenuTexture = new Texture(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.mMenuOkTextureRegion = TextureRegionFactory.createFromAsset(this.mSubMenuTexture, this, "gfx/menu_ok.png", 0, 0);
        this.mMenuBackTextureRegion = TextureRegionFactory.createFromAsset(this.mSubMenuTexture, this, "gfx/menu_back.png", 0, 50);
        this.mEngine.getTextureManager().loadTexture(this.mSubMenuTexture);
        
        this.mEngine.getTextureManager().loadTextures(this.mBackgroundTexture);
    }

    @Override
    public Scene onLoadScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        
        this.mMainScene = new Scene(2);
        this.mMainScene.setBackgroundEnabled(false);
        this.mMainScene.getChild(0).attachChild(new Sprite(0, 0, this.mBackgroundTextureRegion));
        this.createMenuScene();

        /* Just a simple scene with an animated face flying around. */
        
        //this.mMainScene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

        this.mMainScene.setChildScene(this.mMenuScene, false, true, true);
        return this.mMainScene;
    }

    @Override
    public void onLoadComplete() {

    }

//	@Override
//	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
//		if(pKeyCode == KeyEvent.KEYCODE_MENU && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
//			if(this.mMainScene.hasChildScene()) {
//				/* Remove the menu and reset it. */
//				this.mMenuScene.back();
//			} else {
//				/* Attach the menu. */
//				this.mMainScene.setChildScene(this.mMenuScene, false, true, true);
//			}
//			return true;
//		} else {
//			return super.onKeyDown(pKeyCode, pEvent);
//		}
//	}

    @Override
    public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
        Intent intent;
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();

        switch(pMenuItem.getID()) {
            case MENU_START:

                editor.putBoolean("loadGamemode", false);// load game
                // Commit the edits!
                editor.commit();
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                finish();
                intent = new Intent(MenuActivity.this, GameActivity.class);
                startActivity(intent);

                return true;
            case MENU_LOAD:
                editor.putBoolean("loadGamemode", true);// load game
                // Commit the edits!
                editor.commit();

                DisplayMetrics dm1 = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm1);
                finish();
                intent = new Intent(MenuActivity.this, GameActivity.class);
                startActivity(intent);

                return true;
            case MENU_HELP:
//                 intent = new Intent(MenuActivity.this, VideoPlayer.class);
//                 intent.putExtra("ParentID", "UT02");
//                 startActivity(intent);
            	Dialog help = new HelpDialog(this);
            	help.show();
                return true;
            case MENU_SETTING:

                return true;
            case MENU_SCORE:

                return true;
            case MENU_QUIT:
                pMenuScene.setChildSceneModal(this.mSubMenuScene);
                return true;
            case MENU_QUIT_BACK:

                this.mSubMenuScene.back();
                return true;
            case MENU_QUIT_OK:

                int pid = android.os.Process.myPid();
                android.os.Process.killProcess(pid);
                System.exit(0);
                return true;
            default:
                return false;
        }
    }

    // ===========================================================
    // Methods
    // ===========================================================

    protected void createMenuScene() {
         SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
         boolean isSaveGame = settings.getBoolean("saveGamemode", false);

        this.mMenuScene = new MenuScene(this.mCamera);

        final SpriteMenuItem startMenuItem = new SpriteMenuItem(MENU_START, this.mMenuStartTextureRegion);
        startMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mMenuScene.addMenuItem(startMenuItem);
        if (isSaveGame) {
                final SpriteMenuItem loadMenuItem = new SpriteMenuItem(MENU_LOAD, this.mMenuLoadTextureRegion);
                loadMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
                this.mMenuScene.addMenuItem(loadMenuItem);
        }
        final SpriteMenuItem helpMenuItem = new SpriteMenuItem(MENU_HELP, this.mMenuHelpTextureRegion);
        helpMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mMenuScene.addMenuItem(helpMenuItem);

        final SpriteMenuItem settingMenuItem = new SpriteMenuItem(MENU_SETTING, this.mMenuSettingTextureRegion);
        settingMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mMenuScene.addMenuItem(settingMenuItem);

        final SpriteMenuItem scoreMenuItem = new SpriteMenuItem(MENU_SCORE, this.mMenuScoreTextureRegion);
        scoreMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mMenuScene.addMenuItem(scoreMenuItem);

        final SpriteMenuItem quitMenuItem = new SpriteMenuItem(MENU_QUIT, this.mMenuQuitTextureRegion);
        quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        this.mMenuScene.addMenuItem(quitMenuItem);

        this.mMenuScene.buildAnimations();

        this.mMenuScene.setBackgroundEnabled(false);

        this.mMenuScene.setOnMenuItemClickListener(this);



        this.mSubMenuScene = new MenuScene(this.mCamera);
        this.mSubMenuScene.addMenuItem(new SpriteMenuItem(MENU_QUIT_OK, this.mMenuOkTextureRegion));
        this.mSubMenuScene.addMenuItem(new SpriteMenuItem(MENU_QUIT_BACK, this.mMenuBackTextureRegion));
        this.mSubMenuScene.setMenuAnimator(new SlideMenuAnimator());
        this.mSubMenuScene.buildAnimations();
        this.mSubMenuScene.setBackgroundEnabled(false);
        this.mSubMenuScene.setOnMenuItemClickListener(this);

    }

    @Override
    public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
    {
        if(pKeyCode == KeyEvent.KEYCODE_MENU)
        {
            return true;
        }
        else
        {
            return super.onKeyDown(pKeyCode, pEvent);
        }
    }

}
